/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef QTPROJECT_FLUTTEREMBEDQWEIGHT_H
#define QTPROJECT_FLUTTEREMBEDQWEIGHT_H

#include <QtWidgets/QWidget>
#include <QtWidgets/QDialog>
#include <QDebug>
#include <QtCore/qcoreevent.h>
#include "IMessageHandler.h"
#include <functional>
#include <mutex>
#include <condition_variable>
#include <QSharedPointer>

class FlutterEmbeddedQWidget;

class FlutterEmbeddedEventHandler : public EB::IMessageHandler {
public:
    explicit FlutterEmbeddedEventHandler(const QString &name, std::weak_ptr<FlutterEmbeddedQWidget> flutterPagePtr);
    
    void RegisterMessageHandler(std::function<void(const QString &fun, const QString &data)>);
    
    void onMessage(const QString &fun, const QString &data) override;
    
    void NotifyWidgetCreate(QString id, QString routePage)
    {
        invokeMethod(id, "CreateArkTSWindow", routePage);
    }
    
    void NotifyWidgetDestroy(QString id)
    {
        invokeMethod(id, "DestroyArkTsWindow", "");
    }
    
    void setCallBack(std::function<void(const QString&)> cb);
    
    std::function<void(const QString&)> messageCB;
    
    void QTSendFlutter(const QString &id, const QString &data)
    {
        invokeMethod(id, "QTSendFlutter", data);
    }
    
    std::function<void(const QString &fun, const QString &data)> usrMessageHandler_;
    std::weak_ptr<FlutterEmbeddedQWidget> flutterPageWeakPtr;
};

class FlutterEmbeddedQWidget : public std::enable_shared_from_this<FlutterEmbeddedQWidget> {
public:
    /*
      brief : 嵌入qt主键flutter页面工厂创建方法
      parameter： [in]parent  指定被嵌入flutter页面的父节点， route flutter 页面加载默认路由
      return : 返回 FlutterEmbeddedQWidget 的智能指针
    */
    static std::shared_ptr<FlutterEmbeddedQWidget> Create(QWidget* parent, QString route = "index");
    
    /*
       brief:  嵌入qt主键flutter页面工厂销毁方法
       parameter : [in] widget 待销毁的嵌入主键
       return : 无
    */
    static void Destroy(std::shared_ptr<FlutterEmbeddedQWidget>& widget);
    
    ~FlutterEmbeddedQWidget();
    
    std::shared_ptr<FlutterEmbeddedEventHandler> GetEventHandler(void);
    QWidget* GetInnerWidget();
    void RegisterMessageHandler (std::function<void(const QString &fun, const QString &data)>);
    int sendMessageToEmbedded(QString message);
    
    QString getIdStr();
    
    void RegisterHandler(std::shared_ptr<FlutterEmbeddedEventHandler> handler);
    
    void Destroy();
    
    void dispatchEvent(const QEvent* event);

private:
    explicit  FlutterEmbeddedQWidget(QString route);
    void init(QWidget* parent);
    std::shared_ptr<FlutterEmbeddedEventHandler> eventHandler;
    QString flutterRouterPage = "index";
    QString id;
    bool isflutterdestory = false;
    std::mutex mtx;
    std::condition_variable cv;
    QSharedPointer<QWidget> wrapperWidget = nullptr;
    static int g_count;
};
#endif //QTPROJECT_FLUTTEREMBEDQWEIGHT_H

